package com.tl.boot.common.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * com.tl.boot.common.utils -- AesUtil
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/11 9:30
 */
public class AesUtil {

    private static final String ENCODING = "UTF-8";

    private static final String AES_TYPE = "AES/CBC/PKCS5Padding";

    private static final String AES_KEY = "5252325015523623";

    private static final String ivParameter = "cdf9bbacce3a7649";

    /**
     * 加密
     *
     * @param srcString
     * @return
     */
    public static String encrypt(String srcString) {
        return encrypt(srcString, AES_KEY);
    }

    /**
     * 解密
     *
     * @param encryptedStr
     * @return
     */
    public static String decrypt(String encryptedStr) {
        return decrypt(encryptedStr, AES_KEY);
    }

    /**
     * 加密
     *
     * @param srcString
     * @param salt
     * @return
     */
    public static String encrypt(String srcString, String salt) {
        try {
            SecretKeySpec key = new SecretKeySpec(MD5Util.encode(salt, MD5Util.DigitType.LOWER16).getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] encryptedData = cipher.doFinal(srcString.getBytes(ENCODING));
            return Base64.getEncoder().encodeToString(encryptedData);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] aesEncrypt(String content, String pkey) throws Exception {
        SecretKeySpec key = new SecretKeySpec(pkey.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
        //        IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
        return encrypted; // 加密
    }

    public static String aesEncryptStr(String content, String pkey) throws Exception {
        byte[] aesEncrypt = aesEncrypt(content, pkey);
        String base64EncodeStr = org.apache.commons.codec.binary.Base64.encodeBase64String(aesEncrypt);
        return base64EncodeStr;
    }

    /**
     * 解密
     *
     * @param encryptedStr
     * @param salt
     * @return
     */
    public static String decrypt(String encryptedStr, String salt) {
        try {
            byte[] byteMi = Base64.getDecoder().decode(encryptedStr);
            SecretKeySpec key = new SecretKeySpec(MD5Util.encode(salt, MD5Util.DigitType.LOWER16).getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, ENCODING);
        } catch (Exception e) {
            return null;
        }
    }
}

